package com.luoani;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
    test2();
    }
    public static void test2() {
        TreeMap<Student,String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        treeMap.put(new Student("小王",18),"深圳");
        treeMap.put(new Student("小张",19),"北京");
        treeMap.put(new Student("小李",20),"上海");
        treeMap.put(new Student("小赵",21),"广州");
        treeMap.put(new Student("小孙",22),"深圳");
        treeMap.forEach((k,v)-> System.out.println(k + ":" + v));
    }

    private static void test1() {
        TreeMap<Student,String> treeMap = new TreeMap<>();
        treeMap.put(new Student("小王",18),"深圳");
        treeMap.put(new Student("小张",21),"北京");
        treeMap.put(new Student("小李",20),"上海");
        treeMap.put(new Student("小赵",19),"广州");
        treeMap.put(new Student("小孙",22),"深圳");
        treeMap.forEach((k,v)-> System.out.println(k + ":" + v));
    }
}
